package com.demo2do.fms.admin.dao.vehicle;

import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.vehicle.AuditStatus;
import com.demo2do.fms.entity.vehicle.source.SyncStatus;
import com.demo2do.fms.entity.vehicle.source.VehicleSource;

/**
 *
 * @author Ark
 *
 */
@Dao("hibernate")
public interface VehicleSourceDao {
    
    /**
     * @return
     */
    @Query("SELECT COUNT(*) FROM VehicleSource WHERE disabled = false")
    public int count();
    
    /**
     * @param supplier
     * @return
     */
    @Query("SELECT COUNT(*) FROM VehicleSource WHERE disabled = false AND supplier = :supplier")
    public int count(@Variable("supplier") Supplier supplier);
    
    /**
     * @param status
     * @return
     */
    @Query("SELECT COUNT(*) FROM VehicleSource WHERE disabled = false AND status = :status")
    public int count(@Variable("status") SyncStatus status);
    
    /**
     * @param status
     * @param supplier
     * @return
     */
    @Query("SELECT COUNT(*) FROM VehicleSource WHERE disabled = false AND status = :status AND supplier = :supplier")
    public int count(@Variable("status") SyncStatus status, @Variable("supplier") Supplier supplier);
    
    /**
     * @param status
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM VehicleSource WHERE disabled = false AND status = :status")
    public List<VehicleSource> list(@Variable("status") SyncStatus status, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * @param status
     * @param supplier
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM VehicleSource WHERE disabled = false AND status = :status AND supplier = :supplier")
    public List<VehicleSource> list(@Variable("status") SyncStatus status, @Variable("supplier") Supplier supplier, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);

    
    
    // ==================================================
    
    /**
     * @param syncStatus
     * @param auditStatus
     * @return
     */
    @Query("SELECT COUNT(*) FROM VehicleSource WHERE syncStatus = :syncStatus AND status = :auditStatus AND synchronize = false AND organization IS NOT NULL AND disabled = false")
    public int count(@Variable("syncStatus") SyncStatus syncStatus, @Variable("auditStatus") AuditStatus auditStatus);
    
    /**
     * @param supplierIds
     * @return
     */
    @Query("SELECT COUNT(*) FROM VehicleSource WHERE disabled = false AND supplier.id IN (:supplierIds)")
    public int countBySupplierIds(@Variable("supplierIds") List<Long> supplierIds);
    
    /**
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM VehicleSource WHERE disabled = false ORDER BY tractorBrandCode ASC")
    public List<VehicleSource> list(@Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * @param supplier
     * @return
     */
    @Query("FROM VehicleSource WHERE supplier = :supplier AND disabled = false")
    public List<VehicleSource> list(@Variable("supplier") Supplier supplier);
    
    /**
     * @param supplier
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM VehicleSource WHERE supplier = :supplier AND disabled = false ORDER BY tractorBrandCode ASC")
    public List<VehicleSource> list(@Variable("supplier") Supplier supplier, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * @param supplierIds
     * @return
     */
    @Query("FROM VehicleSource WHERE supplier.id IN (:supplierIds) AND disabled = false")
    public List<VehicleSource> listBySupplierIds(@Variable("supplierIds") List<Long> supplierIds);
    
    /**
     * 
     * @param supplierIds
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM VehicleSource WHERE supplier.id IN (:supplierIds) AND disabled = false ORDER BY tractorBrandCode ASC")
    public List<VehicleSource> listBySupplierIds(@Variable("supplierIds") List<Long> supplierIds, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * 
     * @param ids
     * @return
     */
    @Query("FROM VehicleSource WHERE disabled = false AND id IN (:ids)")
    public List<VehicleSource> list(@Variable("ids") List<Long> ids);
    
    /**
     * @param tractorId
     * @return
     */
    @Query("FROM VehicleSource WHERE tractorId = :tractorId AND disabled = false")
    public VehicleSource loadByTractor(@Variable("tractorId") Long tractorId);
    
    /**
     * 
     * @param trailerId
     * @return
     */
    @Query("FROM VehicleSource WHERE trailerId = :trailerId AND disabled = false")
    public VehicleSource loadByTrailer(@Variable("trailerId") Long trailerId);
    
    /**
     * @param code
     * @return
     */
    @Query("FROM VehicleSource WHERE code = :code AND disabled = false")
    public VehicleSource loadByCode(@Variable("code") String code);
    
    /**
     * 
     * @param brand
     * @return
     */
    @Query("FROM VehicleSource WHERE tractorBrandCode = :brand AND code IS NOT NULL AND disabled = true ORDER BY lastModifyTime DESC")
    public VehicleSource loadByBrand(@Variable("brand") String brand);
    
    /**
     * @param tractorBrandCode
     * @return
     */
    @Query("FROM VehicleSource WHERE tractorBrandCode = :tractorBrandCode AND disabled = false")
    public VehicleSource getByTractorCnCode(@Variable("tractorBrandCode") String tractorBrandCode);
    
    /**
     * @param trailerBrandCode
     * @return
     */
    @Query("FROM VehicleSource WHERE trailerBrandCode = :trailerBrandCode AND disabled = false")
    public VehicleSource getByTrailerCnCode(@Variable("trailerBrandCode") String trailerBrandCode);
    
    /**
     * @param simcard
     * @return
     */
    @Query("FROM VehicleSource WHERE simCard = :simCard AND disabled = false")
    public VehicleSource getBySimcard(@Variable("simCard") String simcard);
    
}
